// void, Obj This, Obj other

Unit this;
Settlement sle, invalid;

Sleep(rand(80) + 20);
if(other.AsUnit.IsValid())
{
	str names;
	this = This.AsUnit();

	if(.IsValid){

		if(other.AsUnit.IsHeirOf("Sentry")) return;
		if(other.AsUnit.IsHeirOf("BaseAnimal")) return;
		if(other.AsUnit.IsHeirOf("ShipBattle")) return;
		if(other.AsUnit.IsHeirOf("RamUnit")) return;
		if(other.AsUnit.IsHeirOf("ShipTransport")) return;
		
		.AsWagon.SetName("");
		//.AsWagon.SetName(other.AsUnit.name);
		names = ""+other.AsUnit.name;
		.AsWagon.SetName(names); 
		//pr(.AsWagon.name);
		while(other.IsAlive())
		{
			Obj teleport; teleport = FindTeleport(.player, .posRH, other.posRH);
			if (!.AI) if (teleport.IsValid) if (.posRH.Dist(teleport.pos) > 1000) teleport.Clear;
					
			if (teleport.IsValid) {
				Sleep(rand(80) + 20);
				.AddCommand(true, "follow", other);
				.AddCommand(true, "teleport", teleport);
				return;
			}
			//pr(.AsWagon.HasPath);		
			if(other.AsUnit.InHolder)
			{
				Settlement sett;
							
				sett = other.AsUnit.GetHolderSett();
		 
				if(sett.IsValid)
				{//goto enter
					point pt;
					Building bld;
					if(sle == sett)
					{
						Sleep(3000);
						continue;
					}
					bld = sett.GetCentralBuilding();
					if( bld.IsValid ) {
						if( bld.IsHeirOf( "BaseRuins" )) {
							Sleep(11000);
							.AddCommand(true, "follow", other);
							return;
						} else if( bld.IsHeirOf("Catapult") ) {
							Sleep(1000);
							.AddCommand(true, "follow", other);
							return;
						}
						pt = bld.GetEnterPoint(this);
						while(other.IsAlive())
						{
							if(other.AsUnit.InHolder)
							{
								.SetEntering(true);
								if(sett.GetCentralBuilding.IsHeirOf("BaseShipyard"))
									if(EnvReadString(sett, "tribute_people") == "on"){
										.SetEntering(false);
										Sleep(100);
										.AddCommand(false, "unload", sett.GetCentralBuilding());
										return;
									}
										
								if(.GotoEnter(pt, 0, 1000, false, 0))
								{
									sett.AddUnit(this);
									sle = sett;
									break;
								}
							}
							else break;
						}
					}
				}
				else // Followed unit in ship case
				{
					Ship ship;
					ship = other.AsUnit.GetShipToBoard(); 
					if(ship.IsValid){
						if(other.AsUnit.InHolder)
						{   
							.AddCommand(true, "boardship", ship);
							return;
		  				}
					}
				}	
			}
			else
			{
				sle = invalid;
				.SetEntering(false);
				if(.Goto(other, 360, 1000, true, 0)) Sleep(1500);
			}
		}
	}
}